﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Go.Model;
using Go.Tools;
using tpGo;

namespace Go
{
    public class ModelViewConnection: IObserver
    {
        public static Player actualPlayer;
        public static Boolean playerChange;
        public static Game game;
        public static Tablero tablero;
        public static Boolean isFinish=false;
        public static Player winPlayer;
        public static Position movimientoPendienteParaElModelo;
    
        private ModelViewConnection()
        {
            resetPlayerChange();
            setTableroObserver();
            setIntersectionObserver();
        }

        public static void resetPlayerChange(){
            playerChange=false;
        }
        public void setPlayerChange()
        {
            playerChange = true;
        }

        private void setIntersectionObserver() {
            tablero.setIntersectionObserver(game.Board);
        }

        public static void resetMovimientoPendienteModelo()
        {
            movimientoPendienteParaElModelo = null;
        }
        public static void setMovimientoPendienteModelo(Coordinate aCoord)
        {
            actualPlayer.setMove(game.Board[aCoord]);
        }

        public static void iniciar()
        {
            new ModelViewConnection();
            game.play();
        }

        public static void setGame(Game aGame) {
            game = aGame;
        }

        private void setTableroObserver()
        {
            foreach (Player aPlayer in game.players())
                aPlayer.Register(this);
        }

        public static void gameFinish(Player aPlayer)
        {
            isFinish = true;
            winPlayer = aPlayer;
        }

        public void Update(Object sender)
        {
            setPlayerChange();
            actualPlayer = (Player)sender;
         }

        public static Boolean isActualPlayerHuman() {
            return actualPlayer.isHumanPlayer();
        }

        public static Boolean isValidMove(Coordinate aCoord)
        {
            return RulesValidator.instance().isValidPlayStone(aCoord, actualPlayer);
        }


    }
}
